﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using IOEntity;

namespace IODataAccess
{
    public class clsDAHinhAnh
    {
        clsDAConnection con = new clsDAConnection();
        public clsHinhAnh GetById(int id)
        {
            try
            {
                clsHinhAnh obj = new clsHinhAnh();
                obj.Tour = new clsTour();
                obj.LoaiHinhAnh = new clsLoaiHinhAnh();
                string str = "select * from tblHinhAnh where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    obj.Id = id;
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["url"] != DBNull.Value)
                    {
                        obj.Url = dr["url"].ToString();
                    }
                    if (dr["Loai"] != DBNull.Value)
                    {
                        obj.LoaiHinhAnh.Id = Convert.ToInt32(dr["Loai"].ToString());
                    }
                    if (dr["tourid"] != DBNull.Value)
                    {
                        obj.Tour.Id = Convert.ToInt32(dr["tourid"].ToString());
                    }
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsHinhAnh> GetByTour(int tourid)
        {
            try
            {
                List<clsHinhAnh> list = new List<clsHinhAnh>();
                string str = "select * from tblHinhAnh where tourid=@tourid";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tourid", tourid);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsHinhAnh obj = new clsHinhAnh();
                    obj.Tour = new clsTour();
                    obj.LoaiHinhAnh = new clsLoaiHinhAnh();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["url"] != DBNull.Value)
                    {
                        obj.Url = dr["url"].ToString();
                    }
                    if (dr["Loai"] != DBNull.Value)
                    {
                        obj.LoaiHinhAnh.Id = Convert.ToInt32(dr["Loai"].ToString());
                    }
                    if (dr["tourid"] != DBNull.Value)
                    {
                        obj.Tour.Id = Convert.ToInt32(dr["tourid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsHinhAnh> GetByLoai(int loai)
        {
            try
            {
                List<clsHinhAnh> list = new List<clsHinhAnh>();
                string str = "select * from tblHinhAnh where loai=@loai";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@loai", loai);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsHinhAnh obj = new clsHinhAnh();
                    obj.Tour = new clsTour();
                    obj.LoaiHinhAnh = new clsLoaiHinhAnh();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["url"] != DBNull.Value)
                    {
                        obj.Url = dr["url"].ToString();
                    }
                    if (dr["Loai"] != DBNull.Value)
                    {
                        obj.LoaiHinhAnh.Id = Convert.ToInt32(dr["Loai"].ToString());
                    }
                    if (dr["tourid"] != DBNull.Value)
                    {
                        obj.Tour.Id = Convert.ToInt32(dr["tourid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsHinhAnh> GetByLoaiTour(int loai,int tour)
        {
            try
            {
                List<clsHinhAnh> list = new List<clsHinhAnh>();
                string str = "select * from tblHinhAnh where loai=@loai and tourid=@tour";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@loai", loai);
                com.Parameters.AddWithValue("@tour", tour);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    clsHinhAnh obj = new clsHinhAnh();
                    obj.Tour = new clsTour();
                    if (dr["tieude"] != DBNull.Value)
                    {
                        obj.TieuDe = dr["tieude"].ToString();
                    }
                    if (dr["url"] != DBNull.Value)
                    {
                        obj.Url = dr["url"].ToString();
                    }
                    if (dr["Loai"] != DBNull.Value)
                    {
                        obj.LoaiHinhAnh.Id = Convert.ToInt32(dr["Loai"].ToString());
                    }
                    if (dr["tourid"] != DBNull.Value)
                    {
                        obj.Tour.Id = Convert.ToInt32(dr["tourid"].ToString());
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int Insert(clsHinhAnh obj)
        {
            try
            {
                string str = "insert into tblHinhAnh(tieude,url,loai,tourid) values(@tieude,@url,@loai,@tourid)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@url", obj.Url);
                com.Parameters.AddWithValue("@loai", obj.LoaiHinhAnh.Id);
                if (obj.Tour.Id > 0)
                    com.Parameters.AddWithValue("@tourid", obj.Tour.Id);
                else
                {
                    com.Parameters.AddWithValue("@tourid", DBNull.Value);
                }
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblHinhAnh");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsHinhAnh obj)
        {
            try
            {
                string str = "Update tblHinhAnh set tieude=@tieude,url=@url,loai=@loai,tourid=@tourid where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tieude", obj.TieuDe);
                com.Parameters.AddWithValue("@url", obj.Url);
                com.Parameters.AddWithValue("@loai", obj.LoaiHinhAnh.Id);
                if (obj.Tour.Id > 0)
                    com.Parameters.AddWithValue("@tourid", obj.Tour.Id);
                else
                {
                    com.Parameters.AddWithValue("@tourid", DBNull.Value);
                }
                com.Parameters.AddWithValue("@id", obj.Id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblHinhAnh where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
